/*
 * @LastEditors: 杜康
 * @LastEditTime: 2021-10-01 14:43:30
 */
const _items = Symbol('stackItems')
const _count = Symbol('setCount')
export default class Stack {
    constructor () {
        this[_count] = 0
        this[_items] = []
    }
    push (element) {
        this[_items][this[_count]] = element
        this[_count] += 1
    }
    size () {
        return this[_count]
    }
    isEmpty () {
        return this[_count] === 0
    }
    pop () {
        if (this.isEmpty()) {
            return undefined
        }
        this[_count] -= 1
        const result = this[_items][this[_count]]
        delete this[_items][this[_count]]
        return result
    }
    peek () {
        if (this.isEmpty()) {
            return undefined
        }
        return this[_items][this[_count] - 1]
    }
    clear () {
        // this[_items] = []
        // this[_count] = 0
        // 或者通过LIFO
        while (!this.isEmpty()) {
            this.pop()
        }
    }
    toString () {
        if (this.isEmpty()) {
            return ''
        }
        let objString = `${this[_items][0]}`
        for (let i = 1; i < this[_count]; i++) {
            objString = `${objString},${this.item[i]}`
        }
        return objString
    }
}

const stack = new Stack()
stack.push(5)
stack.push(8)
console.log('🚀 ~ stack.getpropertyNames(stack)', Object.getOwnPropertyNames(stack))
console.log('🚀 ~ Object.keys(stack)', Object.keys(stack))
console.log('🚀 ~ stack[_items]', stack[_items])
// Object.getOwnPropertyNames(stack)[0]
console.log('🚀 ~ Object.getOwnPropertyNames(stack)[0]', Object.getOwnPropertyNames(stack)[0])
// console.log('🚀 ~ stack[_items]', stack[_items])
console.log('🚀 ~ stack', stack)